﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;
using ERPStore.Web.Routing;

namespace ERPStore.Payment.Paypal
{
	public class PluginInit : ERPStore.Extensibility.PluginInit
	{
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			var settings = new Models.Settings();
			settings.BindFromConfiguration(ERPStore.Payment.Paypal.Configuration.ConfigurationSettings.AppSettings);
			Container.RegisterInstance<Models.Settings>(settings, new ContainerControlledLifetimeManager());

			Container.RegisterType<ERPStore.Services.IPaymentService, Services.PaypalExpressService>("paypal", new ContainerControlledLifetimeManager());
		}

		public override void Load()
		{
			base.Load();
		}

		public override void RegisterRoutes()
		{
			var namespaces = new string[] { "ERPStore.Payment.Paypal.Controllers" };

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressPaymentAccepted"
				, "paypal/paiement/accepte"
				, new { controller = "PaypalExpress", action = "BackFromPaypal" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressPaymentDeclined"
				, "paypal/paiement/refuse"
				, new { controller = "PaypalExpress", action = "Declined" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressPaymentError"
				, "paypal/paiement/erreur"
				, new { controller = "PaypalExpress", action = "Error" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressPaymentCanceled"
				, "paypal/paiement/annule"
				, new { controller = "PaypalExpress", action = "Canceled" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressConfirmation"
				, "paypal/paiement/confirmation"
				, new { controller = "PaypalExpress", action = "Confirmation" }
				, namespaces
			);

			RouteTable.Routes.MapRoute(
				"PaypalExpressPostSale"
				, "paypal/postsale"
				, new { controller = "PaypalExpress", action = "Callback" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"PaypalExpressFinalized"
				, "paypal/paiement/merci/{*key}"
				, new { controller = "PaypalExpress", action = "Finalized" , key = string.Empty}
				, namespaces
			);

		}

	}
}
